# 根据BGR的三个通道设计三色的直方图

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("./img/cat.png")

# 颜色值范围 0 ~ 255
x = np.arange(0,256)

def getChannel(channel_img):
    # 设置一个同x一样长度的矩阵
    y = np.zeros(x.shape)
    # 遍历颜色范围值
    h,w = channel_img.shape
    for i in range(h):
        for j in range(w):
            for k in x:
                if channel_img[i][j] == k:
                    y[k] = y[k] + channel_img[i][j] / 255
    return y
# 分别获取三个图层像素直方图
y1 = getChannel(img[:,:,0]) # G
y2 = getChannel(img[:,:,1]) # B
y3 = getChannel(img[:,:,2]) # R

# 绘制整个像素点统计直方图
plt.plot(x,y1,'g-')
plt.plot(x,y2,'b-')
plt.plot(x,y3,'r-')
plt.show()